package com.my.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.domain.MyThicknessData;
import com.my.mapper.MyThicknessDataMapper;
import com.my.vo.ThicknessVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MyThicknessDataService extends ServiceImpl<MyThicknessDataMapper, MyThicknessData> {

    /**
     * 通过纸张id获取点位厚度集合
     *
     * @param id
     * @return
     */
    public List<List<ThicknessVo>> thicknessList(String id,String name) {
        List<List<ThicknessVo>> thicknessVoList = new ArrayList<>();
        List<ThicknessVo> myThicknessDataList = baseMapper.thicknessList(new QueryWrapper<>(MyThicknessData.class)
                .eq(id != null, "mtd.paper_id", id)
                .eq(name != null, "mp.name", name));
        if (CollUtil.isEmpty(myThicknessDataList)) {
            return thicknessVoList;
        }
        Map<Integer, List<ThicknessVo>> groupedByTestNo = myThicknessDataList.stream()
                .collect(Collectors.groupingBy(
                        ThicknessVo::getTestNo,       // 分组依据
                        Collectors.toList()              // 收集为List
                ));

        groupedByTestNo.forEach((k, v) ->
                thicknessVoList.add(v));
        return thicknessVoList;
    }

    public void paperThicknessDeleted(String id) {
        removeById(id);
    }

    public void thicknessEdit(MyThicknessData myThicknessData) {
        saveOrUpdate(myThicknessData);
    }
}
